'1st to Run, get Interval and Duration
Sub ProcessItvAndDur()
Call NewDI2
End Sub

'Finally, extract the data
Sub DataExtraction()

Sheets.Add.Name = "Summary"

Call ExTC
Call ExIts
Call ExDur
Call ExItv
Call TCCount
Call AvgIntensity
Call AvgInterval
Call AvgDuration

Set Summary = ThisWorkbook.Sheets("Summary")

    With Summary
    .Range("B1").Value = "TC Frequency"
    .Range("C1").Value = "Avg Coil Intensity" 'Average Coiling Intensity
    .Range("D1").Value = "Avg Coil Interval" 'Average Coiling Interval
    .Range("E1").Value = "Avg Coil Duration" 'Average Coiling Duration
    End With

End Sub
'Check result manually, edit the result
Sub ManualEditing()

Dim RowNumber As Long
Dim Lastrow As Long, LastColResultArea As Long
Dim LastColumn As Long
Dim Interval As Long
Dim Duration As Long
Dim wsInput As Worksheet
Dim i As Integer
Dim StartThreshold As Double
Dim durationStarts As Boolean
Dim intervalStarts As Boolean
Dim durationRow As Long 'for output
Dim intervalRow As Long

Dim highlightStartRow As Long 'just to highlight durations and intervals (not necessary for function)

Set wsInput = ActiveSheet 'Change sheet name to the sheets that need to be edited

        With wsInput
            Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
            LastColumn = 7 '.Cells(3, .Columns.Count).End(xlToLeft).Column
            
            'Change the equation below using average/median, or change the value manually for duration start and end threshold
            .Range("B1").Value = Application.WorksheetFunction.Median(Range("C3:C" & Lastrow)) 'EndThreshold
            StartThreshold = 1.5 'StartThreshold
            
    'Columns("D:G").Select
    'Selection.Delete Shift:=xlToLeft
    
            'since row2,3 & col5&6 were assumed data-free on other sheets too,if not kindly adjust
            .Cells(2, LastColumn - 1).Value = "ValChg"
            .Cells(2, LastColumn).Value = "CompareAvg"
            .Cells(2, LastColumn + 1).Value = "Interval"
            .Cells(2, LastColumn + 2).Value = "Duration"
            
            'Calculate Increment
            .Range("F3").Value = "0"
            .Range("F4:F" & Lastrow).Formula = "=IF(AND(RC[-3]>0, R[-1]C[-3]>0), ABS(RC[-3]-R[-1]C[-3]), 0)"
            'Compare with average value
            .Range("G3:G" & Lastrow).Formula = "=IF(RC[-4]>R1C2, 1, 0)"
            .Range("H3:I" & Lastrow + 2).ClearContents
            
            'Count duration
            'highlightStartRow = 3 'for highlighting
            Duration = 0: Interval = 0: 'initializing counts
            durationRow = 3: intervalRow = 3 'for outputting duration&interval values: this is going to roll down
            durationStarts = False: intervalStarts = True 'even if frame starts with duration,it should still be working
            
            For RowNumber = 3 To Lastrow
                'check ValueChange >3
                If durationStarts Or .Cells(RowNumber, LastColumn - 1).Value >= StartThreshold Then 'durationstarts-for extra doorway into loop if <=3
                    If .Cells(RowNumber, LastColumn).Value > 0 Then 'Compare to Median or Average (Choose)
                        durationStarts = True 'now in duration
                        Duration = Duration + 1
                        
                        If intervalStarts Then
                            .Cells(intervalRow, LastColumn + 1).Value = Interval 'now durations starts, so write down interval
                            intervalRow = intervalRow + 1 '1 row down
                            intervalStarts = False
                            Interval = 0
                            
                            'for debugging only, remove later
                            '.Range(Cells(highlightStartRow, 1), Cells(RowNumber, LastColumn)).Interior.Color = rgbYellow
                            'highlightStartRow = RowNumber
                        End If
                        
                    Else
                        intervalStarts = True 'now in interval
                        Interval = Interval + 1
                        
                        If durationStarts Then
                            .Cells(durationRow, LastColumn + 2).Value = Duration 'now in interval, so write down duration
                            durationRow = durationRow + 1 'for outputting durationvalue
                            Duration = 0
                            durationStarts = False
                            
                            'for debugging only, remove later
                            '.Range(Cells(highlightStartRow, 1), Cells(RowNumber - 1, LastColumn)).Interior.Color = rgbLawnGreen
                            'highlightStartRow = RowNumber
                        End If
                    End If
                'following line is meant to cover the start of frame to first duration
                ElseIf (Not durationStarts) And .Cells(RowNumber, LastColumn - 1).Value < StartThreshold Then
                    intervalStarts = True
                    Interval = Interval + 1
                End If
                
                'for highlighting and counting the remaining portion as either Duration or Interval
                If RowNumber = Lastrow Then
                    If durationStarts Then
                        .Cells(durationRow, LastColumn + 2).Value = Duration
                    ElseIf intervalStarts Then
                        .Cells(intervalRow, LastColumn + 1).Value = Interval
                    End If
                    '.Range(Cells(highlightStartRow, 1), Cells(RowNumber, LastColumn)).Interior.Color = IIf(durationStarts, rgbLawnGreen, rgbYellow)
                End If
            Next
        End With
End Sub
Sub ExTC()
'TC activity
Sheets.Add.Name = "TC"

Dim wsInput As Worksheet, wsOutput As Worksheet, TC As Worksheet
Dim LRowI As Long
Dim i As Integer

Set wsOutput = ThisWorkbook.Sheets("TC")
i = 1
For Each wsInput In ThisWorkbook.Worksheets
    If wsInput.Name <> wsOutput.Name And wsInput.Name <> "Summary" Then
        With wsInput
            .Range("D3").Value = .Range("D3:D4").Value
            '~~> Get the last row of input sheet
            LRowI = .Range("D" & .Rows.Count).End(xlUp).Row
            '~~> Copy your range
            .Range("D3:D" & LRowI).Copy
            wsOutput.Cells(2, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
            wsOutput.Cells(1, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).Value = ("Fish" & i)
            i = i + 1
        End With
    End If
Next

End Sub

Sub ExIts()
'TC Intensity
Sheets.Add.Name = "Intensity"

Dim wsInput As Worksheet, wsOutput As Worksheet, Intensity As Worksheet
Dim LRowI As Long
Dim i As Integer

Set wsOutput = ThisWorkbook.Sheets("Intensity")

i = 1

For Each wsInput In ThisWorkbook.Worksheets
    If wsInput.Name <> wsOutput.Name And wsInput.Name <> "TC" And wsInput.Name <> "Summary" Then
        With wsInput
            .Range("E3").Value = .Range("E3:E4").Value
            '~~> Get the last row of input sheet
            LRowI = .Range("E" & .Rows.Count).End(xlUp).Row
            '~~> Copy your range
            .Range("E3:E" & LRowI).Copy
            wsOutput.Cells(2, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
                If .Cells(2, wsOutput.Columns.Count).End(xlToLeft).Value = "" Then
                .Cells(2, wsOutput.Columns.Count).End(xlToLeft).Value = 0
                End If
            wsOutput.Cells(1, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).Value = ("Fish" & i)
            i = i + 1
        End With
    End If
Next

End Sub
Sub ExDur()
'TC Duration

Sheets.Add.Name = "Duration"

Dim wsInput As Worksheet, wsOutput As Worksheet
Dim LRowI As Long
Dim i As Integer

Set wsOutput = ThisWorkbook.Sheets("Duration")

i = 1
For Each wsInput In ThisWorkbook.Worksheets
    If wsInput.Name <> wsOutput.Name And wsInput.Name <> "TC" And wsInput.Name <> "Intensity" And wsInput.Name <> "Interval" And wsInput.Name <> "Summary" Then
        With wsInput
        If IsEmpty(.Range("I3")) = True Then
        .Range("I3").Value = 0
        End If
            .Range("I3").Value = .Range("I3:I4").Value
            '~~> Get the last row of input sheet
            LRowI = .Range("I" & .Rows.Count).End(xlUp).Row
                If LRowI <= 2 Then
                LRowI = 3
                End If
            '~~> Copy your range
            .Range("I3:I" & LRowI).Copy
            wsOutput.Cells(2, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
            wsOutput.Cells(1, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).Value = ("Fish" & i)
            i = i + 1
        End With
    End If
Next

End Sub
Sub ExItv()
'TC Interval

Sheets.Add.Name = "Interval"

Dim wsInput As Worksheet, wsOutput As Worksheet, Duration As Worksheet
Dim ItvRow As Long
Dim DurRow As Long
Dim ItvRowFinal As Long
Dim i As Integer

Set wsOutput = ThisWorkbook.Sheets("Interval")

i = 1

For Each wsInput In ThisWorkbook.Worksheets
    If wsInput.Name <> wsOutput.Name And wsInput.Name <> "TC" And wsInput.Name <> "Intensity" And wsInput.Name <> "Duration" And wsInput.Name <> "Summary" Then
        With wsInput
            '~~> Get the last row of input sheet
            ItvRow = .Range("H" & .Rows.Count).End(xlUp).Row
            DurRow = .Range("I" & .Rows.Count).End(xlUp).Row
                If ItvRow = DurRow Then
            .Range("H3").ClearContents
                If ItvRow < 4 Then
                ItvRow = 4
                End If
            .Range("H3:H" & ItvRow - 1).Value = .Range("H4:H" & ItvRow).Value
            .Range("H" & ItvRow).ClearContents
                Else
            .Range("H3").ClearContents
                If ItvRow < 4 Then
                ItvRow = 4
                End If
            .Range("H" & ItvRow).ClearContents
            .Range("H3:H" & ItvRow - 1).Value = .Range("H4:H" & ItvRow).Value
            ItvRow = .Range("H" & .Rows.Count).End(xlUp).Row
                End If
            
            
            '~~> Copy your range
                If IsEmpty(.Range("H3")) = True Then
                .Range("H3").Value = 0
                End If
            LRowFinal = .Range("H" & .Rows.Count).End(xlUp).Row
            .Range("H3:H" & LRowFinal).Copy
                If IsEmpty(.Range("H3")) = True Then
                .Range("H3").Value = 0
                End If
            wsOutput.Cells(2, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
            wsOutput.Cells(1, wsOutput.Columns.Count).End(xlToLeft).Offset(, 1).Value = ("Fish" & i)
            i = i + 1
        End With
    End If
Next

End Sub

Sub TCCount()
'Count TC

'Sheets.Add.Name = "Summary"

Dim wsInput As Worksheet, wsOutput As Worksheet
Dim LastColumn As Long
Dim CountNum As Long
Dim RowNumber As Long, ResultRow As Long
Dim Lastrow As Long
Dim ColNumber As Long
Dim i As Integer

i = 1

'Tail coiling Frequency
Set wsOutput = ThisWorkbook.Sheets("Summary")
Set wsInput = Worksheets("TC")
    
With wsInput
    LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
    FirstRow = .Cells(2, 2).Row
    
For ColNumber = 2 To LastColumn
    Lastrow = .Cells(.Rows.Count, ColNumber).End(xlUp).Row
    
    'Count Tail coiling Frequency
    CountNum = Application.WorksheetFunction.Count(Range(wsInput.Cells(2, ColNumber), wsInput.Cells(Lastrow, ColNumber)))

    'Posting Result (per Minute)
    wsOutput.Cells(wsOutput.Rows.Count, 2).End(xlUp).Offset(1).Value = CountNum * 1 'Adjust according to recording duration (unit is tail coiling/minute)
    
    'Row Names
    wsOutput.Cells(wsOutput.Rows.Count, 1).End(xlUp).Offset(1).Value = ("Fish" & i)
    i = i + 1
Next
End With

End Sub

Sub AvgIntensity()

Dim wsInput As Worksheet, wsOutput As Worksheet
Dim LastColumn As Long
Dim ItsAverage As Variant
Dim aSum As Long
Dim aCount As Long
Dim RowNumber As Long, ResultRow As Long
Dim Lastrow As Long
Dim ColNumber As Long
Dim i As Integer

'Average Intensity
Set wsOutput = ThisWorkbook.Sheets("Summary")
Set wsInput = Worksheets("Intensity")
    
With wsInput
    LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
    FirstRow = .Cells(2, 2).Row
    
For ColNumber = 2 To LastColumn
    Lastrow = .Cells(.Rows.Count, ColNumber).End(xlUp).Row
    
    'Count Tail coiling Frequency
    If Application.WorksheetFunction.Count(Range(wsInput.Cells(2, ColNumber), wsInput.Cells(Lastrow, ColNumber))) > 0 Then
    ItsAverage = Application.WorksheetFunction.Average(Range(wsInput.Cells(2, ColNumber), wsInput.Cells(Lastrow, ColNumber)))
        Else
    ItsAverage = 0
    End If
    'Posting Result Average per minute (30s video)
    wsOutput.Cells(wsOutput.Rows.Count, 3).End(xlUp).Offset(1).Value = ItsAverage
    
Next
End With
End Sub
Sub valueDemo()
    MsgBox Worksheets("Intensity").Range("B2").Value
End Sub
Sub AvgInterval()

Dim wsInput As Worksheet, wsOutput As Worksheet
Dim LastColumn As Long
Dim AvgInterval As Long
Dim RowNumber As Long, ResultRow As Long
Dim Lastrow As Long
Dim ColNumber As Long
Dim i As Integer

'Average Interval
Set wsOutput = ThisWorkbook.Sheets("Summary")
Set wsInput = Worksheets("Interval")
    
With wsInput
    LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
    FirstRow = .Cells(2, 2).Row
    
For ColNumber = 2 To LastColumn
    Lastrow = .Cells(.Rows.Count, ColNumber).End(xlUp).Row
    
    'Count Tail coiling Frequency
    AvgInterval = Application.WorksheetFunction.Average(Range(wsInput.Cells(2, ColNumber), wsInput.Cells(Lastrow, ColNumber)))

    'Posting Result Average per minute(30s video)
    wsOutput.Cells(wsOutput.Rows.Count, 4).End(xlUp).Offset(1).Value = AvgInterval / 30

Next
End With
End Sub
Sub AvgDuration()

Dim wsInput As Worksheet, wsOutput As Worksheet
Dim LastColumn As Long
Dim AvgDuration As Long
Dim RowNumber As Long, ResultRow As Long
Dim Lastrow As Long
Dim ColNumber As Long
Dim i As Integer

'Average Duration
Set wsOutput = ThisWorkbook.Sheets("Summary")
Set wsInput = Worksheets("Duration")
    
With wsInput
    LastColumn = .Cells(2, .Columns.Count).End(xlToLeft).Column
    FirstRow = .Cells(2, 2).Row
    
For ColNumber = 2 To LastColumn
    Lastrow = .Cells(.Rows.Count, ColNumber).End(xlUp).Row
    
    'Count Tail coiling Frequency
    AvgDuration = Application.WorksheetFunction.Average(Range(wsInput.Cells(2, ColNumber), wsInput.Cells(Lastrow, ColNumber)))

    'Posting Result Average per minute(30s video)
    wsOutput.Cells(wsOutput.Rows.Count, 5).End(xlUp).Offset(1).Value = AvgDuration / 30

Next
End With
End Sub

Sub NewDI2()
Dim RowNumber As Long
Dim Lastrow As Long, LastColResultArea As Long
Dim LastColumn As Long
Dim Interval As Long
Dim Duration As Long
Dim wsInput As Worksheet
Dim i As Integer
Dim StartThreshold As Double
Dim durationStarts As Boolean
Dim intervalStarts As Boolean
Dim durationRow As Long 'for output
Dim intervalRow As Long

Dim highlightStartRow As Long 'just to highlight durations and intervals (not necessary for function)

'Set wsInput = ThisWorkbook.Sheets("16")

    For Each wsInput In ThisWorkbook.Worksheets
        With wsInput
            Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
            LastColumn = 7 '.Cells(3, .Columns.Count).End(xlToLeft).Column
            .Range("B1").Value = Application.WorksheetFunction.Average(Range("C3:C" & Lastrow)) * 0.97
            StartThreshold = 2
            
    'Columns("D:G").Select
    'Selection.Delete Shift:=xlToLeft
    
            'since row2,3 & col5&6 were assumed data-free on other sheets too,if not kindly adjust
            .Cells(2, LastColumn - 1).Value = "ValChg"
            .Cells(2, LastColumn).Value = "CompareAvg"
            .Cells(2, LastColumn + 1).Value = "Interval"
            .Cells(2, LastColumn + 2).Value = "Duration"
            
            'Calculate Increment
            .Range("F3").Value = "0"
            .Range("F4:F" & Lastrow).Formula = "=IF(AND(RC[-3]>0, R[-1]C[-3]>0), ABS(RC[-3]-R[-1]C[-3]), 0)"
            'Compare with average value
            .Range("G3:G" & Lastrow).Formula = "=IF(RC[-4]>R1C2, 1, 0)"
            
            'Count duration
            'highlightStartRow = 3 'for highlighting
            Duration = 0: Interval = 0: 'initializing counts
            durationRow = 3: intervalRow = 3 'for outputting duration&interval values: this is going to roll down
            durationStarts = False: intervalStarts = True 'even if frame starts with duration,it should still be working
            
            For RowNumber = 3 To Lastrow
                'check ValueChange >3
                If durationStarts Or .Cells(RowNumber, LastColumn - 1).Value >= StartThreshold Then 'durationstarts-for extra doorway into loop if <=3
                    If .Cells(RowNumber, LastColumn).Value > 0 Then 'Compare to Median or Average (Choose)
                        durationStarts = True 'now in duration
                        Duration = Duration + 1
                        
                        If intervalStarts Then
                            .Cells(intervalRow, LastColumn + 1).Value = Interval 'now durations starts, so write down interval
                            intervalRow = intervalRow + 1 '1 row down
                            intervalStarts = False
                            Interval = 0
                            
                            'for debugging only, remove later
                            '.Range(Cells(highlightStartRow, 1), Cells(RowNumber, LastColumn)).Interior.Color = rgbYellow
                            'highlightStartRow = RowNumber
                        End If
                        
                    Else
                        intervalStarts = True 'now in interval
                        Interval = Interval + 1
                        
                        If durationStarts Then
                            .Cells(durationRow, LastColumn + 2).Value = Duration 'now in interval, so write down duration
                            durationRow = durationRow + 1 'for outputting durationvalue
                            Duration = 0
                            durationStarts = False
                            
                            'for debugging only, remove later
                            '.Range(Cells(highlightStartRow, 1), Cells(RowNumber - 1, LastColumn)).Interior.Color = rgbLawnGreen
                            'highlightStartRow = RowNumber
                        End If
                    End If
                'following line is meant to cover the start of frame to first duration
                ElseIf (Not durationStarts) And .Cells(RowNumber, LastColumn - 1).Value < StartThreshold Then
                    intervalStarts = True
                    Interval = Interval + 1
                End If
                
                'for highlighting and counting the remaining portion as either Duration or Interval
                If RowNumber = Lastrow Then
                    If durationStarts Then
                        .Cells(durationRow, LastColumn + 2).Value = Duration
                    ElseIf intervalStarts Then
                        .Cells(intervalRow, LastColumn + 1).Value = Interval
                    End If
                    '.Range(Cells(highlightStartRow, 1), Cells(RowNumber, LastColumn)).Interior.Color = IIf(durationStarts, rgbLawnGreen, rgbYellow)
                End If
            Next
        End With
    Next
End Sub
